package basic.study.leetcode;

import java.util.HashMap;
import java.util.Stack;

/**
 * @ClassName Easy20
 * @Description 有效的括号
 * @Company inspur
 * @Author Kevin
 * @Date 2020/6/18 9:11
 * @Version 1.0
 */
public class Easy20 {
    HashMap<Character, Character> map = new HashMap<Character, Character>(){{
        put(']', '[');
        put(')', '(');
        put('}', '{');
    }};

    public boolean isValid(String s) {
        return check(s);
    }

    public boolean check(String text) {
        Stack<Character> stack = new Stack<>();
        for (char ch: text.toCharArray()) {
            if (!map.containsKey(ch)) {
                stack.push(ch);
            } else {
                if (stack.isEmpty()) return false;
                else if (map.get(ch) == stack.peek()) stack.pop();
                else return false;
            }
        }
        if (stack.isEmpty()) {
            return true;
        } else {
            return false;
        }
    }
}
